package site.isscloud.jt809;

// VehicleOperationData.java - 车辆运营数据交换(0x1800)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class VehicleOperationData {
    private String vehicleNo;      // 车牌号
    private byte vehicleColor;     // 车牌颜色
    private byte periodType;       // 统计周期(1:日, 2:月, 3:年)
    private String startTime;      // 开始时间(yyyyMMdd/yyyyMM/yyyy)
    private String endTime;        // 结束时间(yyyyMMdd/yyyyMM/yyyy)
    private int operationMileage; // 运营里程(km)
    private int violationCount;    // 违规次数
    private int fuelConsumption;   // 油耗(升)
    
    // 构造方法、getter/setter、toByteArray()和fromByteArray()方法
    public VehicleOperationData(String vehicleNo, byte vehicleColor, byte periodType, 
                               String startTime, String endTime, int operationMileage, 
                               int violationCount, int fuelConsumption) {
        this.vehicleNo = vehicleNo;
        this.vehicleColor = vehicleColor;
        this.periodType = periodType;
        this.startTime = startTime;
        this.endTime = endTime;
        this.operationMileage = operationMileage;
        this.violationCount = violationCount;
        this.fuelConsumption = fuelConsumption;
    }
    
    public byte[] toByteArray() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            dos.write(JT809Utils.stringToBytes(vehicleNo, 21));
            dos.writeByte(vehicleColor);
            dos.writeByte(periodType);
            dos.write(JT809Utils.stringToBytes(startTime, 8));
            dos.write(JT809Utils.stringToBytes(endTime, 8));
            dos.writeInt(operationMileage);
            dos.writeInt(violationCount);
            dos.writeInt(fuelConsumption);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
    
    public static VehicleOperationData fromByteArray(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        DataInputStream dis = new DataInputStream(bais);
        try {
            String vehicleNo = JT809Utils.bytesToString(dis.readNBytes(21));
            byte vehicleColor = dis.readByte();
            byte periodType = dis.readByte();
            String startTime = JT809Utils.bytesToString(dis.readNBytes(8));
            String endTime = JT809Utils.bytesToString(dis.readNBytes(8));
            int operationMileage = dis.readInt();
            int violationCount = dis.readInt();
            int fuelConsumption = dis.readInt();
            
            return new VehicleOperationData(
                vehicleNo, vehicleColor, periodType, startTime, endTime,
                operationMileage, violationCount, fuelConsumption
            );
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public String toString() {
        return "VehicleOperationData{" +
                "vehicleNo='" + vehicleNo + '\'' +
                ", vehicleColor=" + vehicleColor +
                ", periodType=" + periodType +
                ", startTime='" + startTime + '\'' +
                ", endTime='" + endTime + '\'' +
                ", operationMileage=" + operationMileage +
                ", violationCount=" + violationCount +
                ", fuelConsumption=" + fuelConsumption +
                '}';
    }
    
    // Getter和Setter方法
    public String getVehicleNo() { return vehicleNo; }
    public byte getVehicleColor() { return vehicleColor; }
    public byte getPeriodType() { return periodType; }
    public String getStartTime() { return startTime; }
    public String getEndTime() { return endTime; }
    public int getOperationMileage() { return operationMileage; }
    public int getViolationCount() { return violationCount; }
    public int getFuelConsumption() { return fuelConsumption; }
}
